package org.gw.building.ctrl;

import org.apache.commons.lang.StringUtils;
import org.gw.building.model.Message;
import org.gw.building.model.MessageReadState;
import org.gw.building.repositories.MessageReadStateRepository;
import org.gw.building.repositories.MessageRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("message")
public class MessageCtrl {
	@Autowired
	private MessageRepository messageRepository;
	@Autowired
	private MessageReadStateRepository messageReadStateRepository;

	@RequestMapping("queryMessage")
	@ResponseBody
	public Page<Message> queryMessage(String name, String sendType) {
		return messageRepository.query(name, sendType, RequestContextHolder.get().getPageRequest());
	}

	@RequestMapping("addMessage")
	@ResponseBody
	public Object addMessage(Message message) {
		if (StringUtils.isBlank(message.getFsr())) {
			UserInfo user = RequestContextHolder.get().getUser();
			message.setFsr("[" + user.getUid() + "]" + user.getName());
		}
		if (StringUtils.isBlank(message.getFbdx())) {
			message.setFbdx("all");
		}
		message.setFssj(new Date());
		return messageRepository.save(message);
	}

	@RequestMapping("editMessage")
	@ResponseBody
	public Object editMessage(Message message) {
		if (StringUtils.isBlank(message.getFsr())) {
			UserInfo user = RequestContextHolder.get().getUser();
			message.setFsr("[" + user.getUid() + "]" + user.getName());
		}
		if (StringUtils.isBlank(message.getFbdx())) {
			message.setFbdx("all");
		}
		return messageRepository.save(message);
	}

	@RequestMapping("getMessages")
	@ResponseBody
	public Page<Message> getMessages() {
		UserInfo user = RequestContextHolder.get().getUser();
		return messageRepository.findByFbdxInOrderByFssjDesc(Arrays.asList(user.getUid(), user.getDepartment(), "all"),
				RequestContextHolder.get().getPageRequest());
	}

	@RequestMapping("markRead")
	@ResponseBody
	public Object markRead(String messageId) {
		MessageReadState messageReadState = new MessageReadState();
		Message message = new Message();
		message.setId(messageId);
		messageReadState.setMessage(message);
		messageReadState.setYdsj(new Date());
		messageReadState.setYhId(RequestContextHolder.get().getUser().getUid());
		messageReadState.setStatus("1");
		return messageReadStateRepository.save(messageReadState);
	}

	@RequestMapping("markAllRead")
	@ResponseBody
	public Object markAllRead() {
		return null;
	}

	@RequestMapping("getReadState")
	@ResponseBody
	public Object getReadState(String[] messageId) {
		if (messageId == null) {
			return null;
		}
		return messageReadStateRepository.findMessageIdAndStatusByMessageIdInAndYhId(Arrays.asList(messageId),
				RequestContextHolder.get().getUser().getUid());
	}

}
